#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>

using namespace std;

// 有效的变换至多将在第n 轮截止

    int n;
int q[500][2010];
int cnt[2010];

bool check(int k){
    for (int i = 0; i < n; i ++){
        if(q[k - 1][i] != q[k][i]){
            return false;
        }
    }
    return true;
}

int f(){
    int depth = 0;
    while(true){
        memset(cnt, 0, sizeof cnt);
        if(depth && check(depth)){
            return depth;
        }

        for (int i = 0; i < n; i ++){
            cnt[q[depth][i]]++;
        }

        for (int i = 0; i < n; i ++){
            q[depth + 1][i] = cnt[q[depth][i]];
        }

        depth++;
    }
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    int T;
    cin >> T;
    while(T--){
        cin >> n;
        for (int i = 0; i < n; i ++){
            cin >> q[0][i];
        }
        int depth = f();

        int k;
        cin >> k;
        int x, y;
        for (int i = 0; i < k; i ++){
            cin >> x >> y;
            if(y >= depth){
                cout << q[depth][x - 1] << endl;
            }else{
                cout << q[y][x - 1] << endl;
            }
        }
    }

    return 0;
}